package com.gennlife.dataformat.controller;

import com.gennlife.common.utils.PropertiesUtil;
import com.gennlife.common.utils.StringUtils;
import com.gennlife.dataformat.domain.OperationInfo;
import com.gennlife.dataformat.domain.PageResolveConfig;
import com.gennlife.dataformat.service.OperationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.script.ScriptException;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("operation")
public class OperationController {
    private static final Logger logger = LoggerFactory.getLogger(OperationController.class);

    @Autowired
    OperationService operationService;

    //获取所有用户
    @ResponseBody
    @RequestMapping("/getAll")
    public List<OperationInfo> getAll() {
        return operationService.getAll();
    }

    //获取配置
    @ResponseBody
    @RequestMapping("/getConfig")
    public PageResolveConfig getConfig() {
        PageResolveConfig config = new PageResolveConfig();
        config.setSourceFolder(PropertiesUtil.getValue("page.sourceFolder"));
        config.setResolvedFolder(PropertiesUtil.getValue("page.resolvedFolder"));
        config.setResultFolder(PropertiesUtil.getValue("page.resultFolder"));
        config.setResultType(PropertiesUtil.getValue("page.resultType"));
        return config;
    }

    //修改配置
    @ResponseBody
    @RequestMapping("/setConfig")
    public int setConfig(PageResolveConfig config) {
        if (StringUtils.isNotEmpty(config.getSourceFolder())) {
            PropertiesUtil.setValue("page.sourceFolder", config.getSourceFolder());
        }
        if (StringUtils.isNotEmpty(config.getResolvedFolder())) {
            PropertiesUtil.setValue("page.resolvedFolder", config.getResolvedFolder());
        }
        if (StringUtils.isNotEmpty(config.getResultFolder())) {
            PropertiesUtil.setValue("page.resultFolder", config.getResultFolder());
        }
        if (StringUtils.isNotEmpty(config.getResultType())) {
            PropertiesUtil.setValue("page.resultType", config.getResultType());
        }
        return 0;
    }

    //解析数据
    @ResponseBody
    @RequestMapping("/resolveData")
    public String resolveData() {
        Long importCount = 0L;
        String sourceFolder = PropertiesUtil.getValue("page.sourceFolder");
        File file = new File(sourceFolder);
        String[] fs = file.list();    //遍历path下的文件和目录，放在File数组中
        logger.info("一共需要处理" + (fs.length-2) + "个文件。");
        long time1 = System.currentTimeMillis();
        HashSet<String> importFileNames = new HashSet<>(Arrays.asList(fs));
        for (String fileName : fs) {
            operationService.resolveData(new File(sourceFolder + "/" + fileName));
        }
        logger.info("本次导入一共耗时：" + ((System.currentTimeMillis() - time1) / 1000) + "秒");
        return "Success";
    }

    //解析数据
    @ResponseBody
    @RequestMapping("/resolveDataTest")
    public String resolveDataTest(@RequestParam String line,@RequestParam String jsCode) throws ScriptException, NoSuchMethodException {
        return operationService.resolveDataTest(line,jsCode);
    }

}
